#region Using directives

/*
 * Copyright (c) 2005-2007 Alan Dean.
 * All rights reserved.
 */

using System;
using System.Collections.Generic;
using System.Globalization;

using ObjectThinking.Globalization;
using ObjectThinking.Xml.ResourceDescriptionFramework;

#endregion

namespace ObjectThinking
{
    public sealed class ObjectRequiredRule : RuleBase
    {
        private static readonly Uri ObjectKey = new Uri("urn:object-key");
        private static readonly Uri ObjectName = new Uri("urn:object-name");

        // public constructors

        public ObjectRequiredRule(Uri key, string name)
        {
            this[ObjectKey] = key;
            this[ObjectName] = name;
        }

        // public methods (IRule)

        public override bool Validate(ISelf obj)
        {
            return Validate(obj, this[ObjectKey] as Uri);
        }

        // private methods

        private bool Validate(ISelf obj, Uri key)
        {
            if (obj == null) throw new ArgumentNullException();
            if (key == null) throw new ArgumentNullException();

            Rule.ClearViolations(this);

            string name = (this.ContainsKey(ObjectName)) ? this[ObjectName].ToString() : null as string;

            if (obj[key] == null) this[key] = new ObjectRequiredViolation(name);

            return Rule.IsValid(this);
        }
    }
}